package com.iplant.wms.serviceImpl.dao.mss;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.mss.MSSTransferProp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class MSSTransferPropDAO extends BaseDAO {

    private static MSSTransferPropDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(MSSTransferPropDAO.class);


    private MSSTransferPropDAO() {
        super();

        setTableName("mss_transferprop");
    }

    public static MSSTransferPropDAO getInstance() {
        if (Instance == null) {
            Instance = new MSSTransferPropDAO();
        }
        return Instance;
    }



    public void MSS_UpdateTransferProp(
            BMSEmployee wLoginUser, MSSTransferProp wTransferProp, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wTransferProp == null || StringUtils.isEmpty(wTransferProp.Name)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            MSSTransferProp wTransferPropO = this.MSS_CheckTransferProp(wLoginUser, wTransferProp, wErrorCode);
            if (wTransferPropO.ID > 0) {
                if (wTransferProp.ID <= 0) {
                    wTransferProp.ID = wTransferPropO.ID;
                } else {
                    wErrorCode.set(ExceptionTypes.Duplication.getValue());
                }

            }
            if (wErrorCode.Result != 0) {
                return;
            }

            Map<String, Object> wParwms = new HashMap<String, Object>();

            wParwms.put("Name", wTransferProp.Name);
            wParwms.put("Code", wTransferProp.Code);
            wParwms.put("Description", wTransferProp.Description);
            wParwms.put("RuleID", wTransferProp.RuleID);
            wParwms.put("SortIndex", wTransferProp.SortIndex);
            wParwms.put("CompareTypeID", wTransferProp.CompareTypeID);

            wParwms.put("EditorID", wLoginUser.getID());
            wParwms.put("EditTime", Calendar.getInstance());
            wParwms.put("Active", wTransferProp.Active);

            if (wTransferProp.ID > 0) {
                wParwms.put("ID", wTransferProp.ID);
                super.Update(super.getTableName("mss_transferprop"), "ID", wParwms);
            } else {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());


                wParwms.put("CreatorID", wLoginUser.getID());
                wParwms.put("CreateTime", Calendar.getInstance());
                wTransferProp.ID = super.Insert(super.getTableName("mss_transferprop"), wParwms);
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_UpdateTransferProp", ex);
        }
    }


    public void MSS_DeleteTransferProp(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParwms = new HashMap<>();
            wDeleteParwms.put("RuleID", wRuleID);
            this.Delete(wDeleteParwms, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_DeleteTransferProp", ex);
        }
    }


    public void MSS_ActiveTransferProp(
            BMSEmployee wLoginUser, MSSTransferProp wTransferProp, int wActive, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        // 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

        try {
            if (wTransferProp == null || wTransferProp.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            Map<String, Object> wParwms = new HashMap<String, Object>();

            wParwms.put("ID", wTransferProp.ID);
            wParwms.put("EditorID", wLoginUser.getID());
            wParwms.put("EditTime", Calendar.getInstance());
            wParwms.put("Active", wActive);
            super.Update(super.getTableName("mss_transferprop"), "ID", wParwms);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());

            logger.error("MSS_ActiveTransferProp", ex);
        }
    }


    public List<MSSTransferProp> MSS_QueryTransferPropList(
            BMSEmployee wLoginUser, int wRuleID, String wRuleCode, int wBatchRuleID, String wBatchRuleCode,
            int wBatchPropID, String wBatchPropCode, String wName, String wCode, List<Integer> wActive,
            Pagination wPagination, OutResult<Integer> wErrorCode) {
        List<MSSTransferProp> wMSSTransferPropList = new ArrayList<MSSTransferProp>();
        wErrorCode.set(0);

        try {


            if (wActive == null) {
                wActive = new ArrayList<>();
            }

            wActive.removeIf(p -> p <= 0);
            // Step02:查询
            Map<String, Object> wParwms = new HashMap<String, Object>();
            String wSQLText = "";
            wSQLText = StringUtils.Format("Select t.*,t1.Name as RuleName,t1.Code as RuleCode,t1.BatchRuleID, " +
                                          " t2.Name as BatchRuleName,t2.Code as BatchRuleCode, t5.Name as BatchPropName," +
                                          " t5.Code as BatchPropCode,t3.Name as CreatorName ,t4.Name as EditorName  from {0}.mss_transferprop t  " +
                                          " left join {0}.mss_transferrule t1 on  t.RuleID=t1.ID " +
                                          " left join {0}.mss_batchprop t5 on  t.BatchPropID=t5.ID " +
                                          " left join {0}.mss_batchrule t2 on  t1.BatchRuleID=t2.ID  and t5.RuleID=t2.ID" +
                                          " left join {0}.mbs_user t3 on t.CreatorID=t3.ID " +
                                          " left join {0}.mbs_user t4 on t.EditorID=t4.ID " +
                                          " where   (:Code =''''  or  t.Code=:Code) " +
                                          " AND (:RuleID<=0 or  t.RuleID=:RuleID) and  (:RuleCode =''''  or  t1.Code=:RuleCode) " +
                                          " AND (:BatchRuleID<=0 or  t1.BatchRuleID=:BatchRuleID) " +
                                          " AND  (:BatchRuleCode =''''  or  t2.Code=:BatchRuleCode) " +
                                          " AND (:BatchPropID<=0 or  t.BatchPropID=:BatchPropID) " +
                                          " AND  (:BatchPropCode =''''  or  t5.Code=:BatchPropCode) " +
                                          " AND (:Active = '''' or  t.Active in ({1}) ) AND (:Name = '''' or  t.Name like :Name) ",
                    super.getDBName(), wActive.size() > 0 ? StringUtils.Join(",", wActive) : "0");

            wSQLText = this.DMLChange(wSQLText);

            wParwms.put("Code", wCode);
            wParwms.put("RuleID", wRuleID);
            wParwms.put("RuleCode", wRuleCode);
            wParwms.put("BatchRuleID", wBatchRuleID);
            wParwms.put("BatchRuleCode", wBatchRuleCode);
            wParwms.put("BatchPropID", wBatchPropID);
            wParwms.put("BatchPropCode", wBatchPropCode);
            wParwms.put("Active", StringUtils.Join(",", wActive));
            wParwms.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));

            wMSSTransferPropList = super.QueryForList(wSQLText, wParwms, wPagination, MSSTransferProp.class);

            // Step03:配置系统
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryTransferPropList", ex);
        }
        return wMSSTransferPropList;
    }


    public MSSTransferProp MSS_QueryTransferProp(
            BMSEmployee wLoginUser, int wRuleID, String wRuleCode, String wCode, OutResult<Integer> wErrorCode) {
        MSSTransferProp wResult = new MSSTransferProp();

        try {
            if (StringUtils.isEmpty(wCode) || (wRuleID <= 0 && StringUtils.isEmpty(wCode))) {
                wErrorCode.Result = ExceptionTypes.Parameter.getValue();
                return wResult;
            }

            List<MSSTransferProp> wMSSTransferPropList = wMSSTransferPropList = this.MSS_QueryTransferPropList(
                    wLoginUser, wRuleID, wRuleCode, -1, "", -1, "", "", wCode, null, Pagination.getDefault(),
                    wErrorCode);


            if (wErrorCode.Result != 0) {
                return wResult;
            }

            if (wMSSTransferPropList != null && wMSSTransferPropList.size() > 0) {
                wResult = wMSSTransferPropList.get(0);
            }


        } catch (Exception ex) {

            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_QueryTransferProp", ex);
        }
        return wResult;

    }


    public MSSTransferProp MSS_CheckTransferProp(
            BMSEmployee wLoginUser, MSSTransferProp wMSSTransferProp, OutResult<Integer> wErrorCode) {
        MSSTransferProp wTransferProp = new MSSTransferProp();
        wErrorCode.set(0);

        try {

            // Step0:查询
            Map<String, Object> wParwms = new HashMap<String, Object>();
            String wSQLText = StringUtils.Format("Select * from {0}.mss_transferprop " +
                                                 " where ID!=:ID and ( RuleID=:RuleID and  ( Name =:Name or  Code=:Code)) ",
                    super.getDBName());
            wParwms.clear();
            wParwms.put("ID", wMSSTransferProp.ID);
            wParwms.put("Name", wMSSTransferProp.Name);
            wParwms.put("Code", wMSSTransferProp.Code);
            wParwms.put("RuleID", wMSSTransferProp.RuleID);

            wSQLText = this.DMLChange(wSQLText);
            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParwms);
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wTransferProp.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wTransferProp.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wTransferProp.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

                wTransferProp.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wTransferProp.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wTransferProp.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wTransferProp.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wTransferProp.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("MSS_CheckTransferProp", ex);
        }
        return wTransferProp;
    }

}
